package com.tenglong.teacher.ui.dialog;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.utils.ClipboardUtil;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.DataState;

/**
 * A simple {@link Fragment} subclass.
 */
public class CreateClassDialogFragment extends DialogFragment implements View.OnClickListener {


    public static final String KEY_CLASS_CODE = "key_class_code";
    private TextView name;
    private TextView content;
    private String classCode;
    private Context mContext;

    public CreateClassDialogFragment() {
        // Required empty public constructor
    }
    public static CreateClassDialogFragment getInstance(String classCode){
        CreateClassDialogFragment f =new CreateClassDialogFragment();
        Bundle param = new Bundle();
        param.putSerializable(KEY_CLASS_CODE,classCode);
        f.setArguments(param);
        return f;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
        classCode = getArguments().getString(KEY_CLASS_CODE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        return inflater.inflate(R.layout.fragment_create_class_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListeners(view);
    }

    private void setListeners(View view) {
        view.findViewById(R.id.tvCreate).setOnClickListener(this);
        content.setOnClickListener(this);
    }

    private void initView(View view) {
        name = view.findViewById(R.id.name);
        name.setText("尊敬的的"+ DataState.getInstance().getUserInfo().getUserName() +"老师：");
        content = view.findViewById(R.id.content);
        SpannableStringBuilder builder = new SpannableStringBuilder("您的班级号是"+classCode+",学生下载吉鹿学习学生端，填写班级号加入班级即可(点击班级号即可复制)");
        builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext,R.color.theme_green)), 6, 6 + (classCode + "").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext,R.color.app_red)), 6 + (classCode + "").length()+24, 6 + (classCode + "").length()+35, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        content.setText(builder);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvCreate:
                ((Activity)mContext).finish();
                dismiss();
                break;
            case R.id.content:
                ClipboardUtil.clipboard(classCode,mContext);
                ToastUtils.showCenterToast("班级号已复制到剪切板");
                break;
        }
    }
}
